package com.jumei.flume.parser;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.jumei.flume.bo.ShuabaoSensorMessage;
import org.apache.commons.collections.MapUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

import static com.jumei.flume.common.StringUtils.safe4Param;


/**
 * 解析神策kafka消息
 * 
 * 
		time
		date
		user_id
		distinct_id
		imei
		idfa
		$lib
		$os
		$ip
		$lib_version
		$app_version
		$os_version
		$browser_version
		$country
		$is_first_time
		$is_first_day
		$province
		$city
		$referrer
		$manufacturer
		$model
		$carrier
		$screen_name
		ab
		app_source
		appid
		card_type
		category_v3_1
		category_v3_1_name
		category_v3_2
		category_v3_2_name
		category_v3_3
		category_v3_3_name
		current_page_url
		delivery_mode
		friendly_name
		fromSrc
		install_source
		is_return
		item_id
		item_type
		material_item_id
		material_link
		material_order
		material_page_type
		pageflag
		product_id
		ref
		setup_source
		sku_category_id
		sku_category_name
		brand_id
		brand_name
		material_id
		material_name
		material_page

 * 
 * 
 * @author yihongx
 *
 */
public class JSONStringToShuabaoSensorMessageConverter implements Converter<String, ShuabaoSensorMessage> {
	private final static Logger LOGGER = LoggerFactory.getLogger(JSONStringToShuabaoSensorMessageConverter.class);

	/**
	 * 替换 BeanUtils.copyProperties
	 * 
	 * @param sensorMessage
	 * @param properties
	 */
	private void setValues(ShuabaoSensorMessage sensorMessage, JSONObject properties) {

		//PART I
		sensorMessage.setApp_version(properties.getString("$app_version"));
		sensorMessage.setIp(properties.getString("$ip"));
		sensorMessage.setCountry(properties.getString("$country"));
		sensorMessage.setCity(properties.getString("$city"));
		sensorMessage.setProvince(properties.getString("$province"));
		sensorMessage.setLib(properties.getString("$lib"));
		sensorMessage.setLib_version(properties.getString("$lib_version"));
		sensorMessage.setManufacturer(properties.getString("$manufacturer"));
		sensorMessage.setModel(properties.getString("$model"));
		sensorMessage.setOs(properties.getString("$os"));
		sensorMessage.setOs_version(properties.getString("$os_version"));
		sensorMessage.setScreen_height(properties.getInteger("$screen_height"));
		sensorMessage.setScreen_width(properties.getInteger("$screen_width"));
		
		Boolean wifi = MapUtils.getBoolean(properties, "$wifi", Boolean.FALSE);
		sensorMessage.setWifi(wifi?1:null);
		sensorMessage.setBrowser(properties.getString("$browser"));
		sensorMessage.setBrowser_version(properties.getString("$browser_version"));
		sensorMessage.setCarrier(properties.getString("$carrier"));
		sensorMessage.setNetwork_type(properties.getString("$network_type"));
		sensorMessage.setUtm_matching_type(properties.getString("$utm_matching_type"));
		sensorMessage.setLatest_referrer(properties.getString("$latest_referrer"));
		sensorMessage.setLatest_referrer_host(properties.getString("$latest_referrer_host"));
		sensorMessage.setLatest_utm_source(properties.getString("$latest_utm_source"));
		sensorMessage.setLatest_utm_medium(properties.getString("$latest_utm_medium"));
		sensorMessage.setLatest_utm_term(properties.getString("$latest_utm_term"));
		sensorMessage.setLatest_utm_content(properties.getString("$latest_utm_content"));
		sensorMessage.setLatest_utm_campaign(properties.getString("$latest_utm_campaign"));
		sensorMessage.setLatest_search_keyword(properties.getString("$latest_search_keyword"));
		sensorMessage.setLatest_traffic_source_type(properties.getString("$latest_traffic_source_type"));
		
		Boolean is_first_day = MapUtils.getBoolean(properties, "$is_first_day", Boolean.FALSE);
        sensorMessage.setIs_first_day(is_first_day?1:null);
        
        Boolean is_first_time = MapUtils.getBoolean(properties, "$is_first_time", Boolean.FALSE);
        sensorMessage.setIs_first_time(is_first_time?1:null);
        
		sensorMessage.setDevice_id(properties.getString("$device_id"));
		sensorMessage.setImei(properties.getString("$imei"));
		
		//PART II
		sensorMessage.setAd_pos(properties.getString("ad_pos"));
		sensorMessage.setAd_plan_id(properties.getString("ad_plan_id"));
		sensorMessage.setAd_material_id(properties.getString("ad_material_id"));
		sensorMessage.setClick_pos(properties.getString("click_pos"));
		sensorMessage.setAd_app_id(properties.getString("ad_app_id"));
		sensorMessage.setAd_material_id(properties.getString("ad_material_id"));
		sensorMessage.setAd_material_type(properties.getString("ad_material_type"));
		sensorMessage.setAd_material_content(properties.getString("ad_material_content"));
		sensorMessage.setAd_target_url(properties.getString("ad_target_url"));
		sensorMessage.setAd_relate_data(properties.getString("ad_relate_data"));
		sensorMessage.setAd_show_time(properties.getString("ad_show_time"));
		
		//PART III
		Boolean resume_from_background = MapUtils.getBoolean(properties, "$resume_from_background", Boolean.FALSE);
		sensorMessage.setResume_from_background(resume_from_background?1:0);
		sensorMessage.setNetwork_type(properties.getString("$network_type"));
		sensorMessage.setPush_channel(properties.getString("push_channel"));
		sensorMessage.setPush_uniqid(properties.getString("push_uniqid"));
		sensorMessage.setPush_job_id(properties.getString("push_job_id"));
		sensorMessage.setPush_url(properties.getString("push_url"));
		
		sensorMessage.setDevice_id(properties.getString("$device_id"));
		sensorMessage.setCurrent_time(properties.getString("current_time"));
		sensorMessage.setCurrent_url(properties.getString("current_url"));
		sensorMessage.setTitle(properties.getString("$title"));
		sensorMessage.setHas_finished(properties.getString("has_finished"));
		sensorMessage.setDuration(properties.getString("duration"));
		sensorMessage.setIscomment(properties.getString("iscomment"));
		sensorMessage.setIsconcern(properties.getString("isconcern"));
		sensorMessage.setIs_like(properties.getString("is_like"));
		sensorMessage.setIsmine(properties.getString("ismine"));
		sensorMessage.setIsnewuser(properties.getString("isnewuser"));
		sensorMessage.setIssuccess(properties.getString("issuccess"));
		
		sensorMessage.setPage_title(properties.getString("page_title"));
		sensorMessage.setPage_source(properties.getString("page_source"));
		sensorMessage.setElement_name(properties.getString("element_name"));
		sensorMessage.setElement_type(properties.getString("element_type"));
		sensorMessage.setRelease_video_user_id(properties.getString("release_video_user_id"));
		
		//PART IV
		sensorMessage.setPlatform(properties.getString("platform"));
		sensorMessage.setUtm_source(properties.getString("$utm_source"));
		sensorMessage.setEvent_duration(properties.getInteger("event_duration"));
		sensorMessage.setIdfa(properties.getString("idfa"));
		sensorMessage.setReferrer(properties.getString("referrer"));
		sensorMessage.setVideo_content_id(properties.getString("video_content_id"));
		sensorMessage.setSlide_way(properties.getString("slide_way"));
		sensorMessage.setConcern_referrer(properties.getString("concern_referrer"));
		sensorMessage.setComment_referrer(properties.getString("comment_referrer"));
		sensorMessage.setCommentary(properties.getString("commentary"));
		sensorMessage.setLike_referrer(properties.getString("like_referrer"));
		sensorMessage.setNumber_of_clicks(properties.getString("number_of_clicks"));
		sensorMessage.setNumber_of_arrive(properties.getString("number_of_arrive"));
		sensorMessage.setRegister_channel(properties.getString("register_channel"));
		sensorMessage.setRegister_source(properties.getString("register_source"));
		sensorMessage.setAmount(properties.getString("amount"));
		sensorMessage.setIs_logined(properties.getString("is_logined"));
		sensorMessage.setPosition(properties.getString("position"));
		//sensorMessage.setUser_id(user_id);
		sensorMessage.setUrl(properties.getString("$url"));
		
	}
	
    @Override
    public ShuabaoSensorMessage convert(String message) {
    	ShuabaoSensorMessage sensorMessage = new ShuabaoSensorMessage();
    	
    	sensorMessage.setKudu_updated_time(DateTime.now().getMillis());
        
    	/**
    	 * 解析神策kafka消息
    	 */
    	//Primary Key
        JSONObject record = JSON.parseObject(message);
        
        String distinctId = record.getString("distinct_id");
        if(Strings.isNullOrEmpty(distinctId)) {
        	return null;
        }
        
        sensorMessage.setDistinctId(distinctId);
        
        Long time = record.getLong("time");
        sensorMessage.setTime(time);
        
        DateTime dateTime = new DateTime(time);
        sensorMessage.setCreateDate(Integer.valueOf(dateTime.toString("yyyyMMdd")));
        
        String project = record.getString("project");
        sensorMessage.setProject(project);

        String event = record.getString("event");
        sensorMessage.setEvent(safe4Param(event, "unknown"));
        
        Long userId = record.getLong("user_id");
        sensorMessage.setUser_id(safe4Param(userId, -1L));
        
        //Biz Info
        JSONObject properties = record.getJSONObject("properties");
        if(null == properties || properties.isEmpty()) {
        	return sensorMessage;
        }
        
        setValues(sensorMessage, properties);
        
        return sensorMessage;
    }
    
    
    
}
